import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import Qt.UdpServer 1.0

Window {
    id: root;
    visible: true;
    width: 600;
    height: 300;
    title: qsTr("Lock_Demo");
    property int index: 0;
    property int lockCmd: 2;
    property var sendLockID: [];
    property var lockArr: [];
    property var logString: [];
    Column {
        x: 10;
        //y: 10;
        spacing: 10;
        GroupBox {
            id: consoleGrop;
            title: "Status";
            width: root.width - 20;
            height: 100;
            Column{
                spacing: 10;
                Row {
                    id: idRow;
                    x: 10;
                    y: 10;
                    spacing: 5;
                    Text {
                        y: 6;
                        text: qsTr("ID:")
                    }
                    ComboBox {
                        id: idCombox;
                        width: consoleGrop.width / 2 - 50;
                        height: 25;
                        model: ListModel {
                            id: model;
                        }
                        onCurrentIndexChanged: {
                            var lockID = currentText;
                            var arr = findId(lockID);
                            if(arr != null) {
                                updateSta(arr);
                            }
                        }
                    }
                    Text {
                        id: ipText;
                        y: 6;
                        text: qsTr("  IP:")
                    }
                    Text {
                        id: ipCombox;
                        y: ipText.y + 1;
                        width: idCombox.width;
                        height: 25;
                    }
                }
                Row {
                    spacing: 15;
                    x: idRow.x;
                    Rectangle {
                        id: lidRec;
                        radius: 5;
                        width: consoleGrop.width / 3 - 40;
                        height: consoleGrop.height - idCombox.height - 40;
                        color: "transparent";
                        border.color: "lightsteelblue";
                        Text {
                            id: lidText;
                            text: qsTr("Lid_sta");
                            anchors.centerIn: parent;
                        }
                    }
                    Rectangle {
                        id: waterRec;
                        radius: 5;
                        width: lidRec.width;
                        height: lidRec.height;
                        color: "transparent";
                        border.color: "lightsteelblue";
                        Text {
                            id: waterText;
                            text: qsTr("Water_sta");
                            anchors.centerIn: parent;
                        }
                    }
                    Rectangle {
                        id: voltageRec;
                        radius: 5;
                        width: lidRec.width;
                        height: lidRec.height;
                        color: "transparent";
                        border.color: "lightsteelblue";
                        Text {
                            id: voltageText;
                            text: qsTr(" ");
                            anchors.centerIn: parent;
                        }
                    }
                    Button {
                        id: clearBut;
                        y: 15;
                        width: 40;
                        height: lidRec.height / 2;
                        text: "Clear";
                        onClicked: {
                            logText.text = "";
                        }
                    }
                }
            }
        }
        TextArea {
            id: logText;
            readOnly: true;
            width: consoleGrop.width;
            height: root.height - consoleGrop.height - 20;
        }
    }
    function recvLog(data) {
        logText.append(data);
    }
    function sendComand(cmd) {
        var arr = findId(idCombox.currentText);
        arr.cmd = cmd;
    }
    function findId(id) {
           var i;
           for (i = 0; i < lockArr.length; i++) {
               if (id == lockArr[i].id) {
                   return lockArr[i];
               }
           }
           return null;
       }

    function hexTo10(a, b) {
        var ab;
        ab = (a << 8) + b;
        return ab/100;
    }
    function hexRepair(dataString) {
        if (dataString.length == 1) {
            dataString = "0" + dataString;
        }
        return dataString;
    }
       function newLockArrItem(id, cmd, state) {
           var vol = hexTo10(state[16], state[17]);
           var sta = {waterSta: state[15] & 1,  voltage: vol, lidSta: (state[15] & (1 << 1)) >> 1};
           var o = {id : id, cmd : cmd, state: sta };
           lockArr.push(o);
           return o;
       }
       //更新界面显示
       function updateSta(lockArr) {
           //0为未通水、1通水
           switch (lockArr.state.waterSta) {
           case 0:
               waterRec.color =  "transparent";
               waterText.text = "未通水";
               logString[0] = "未通水";
               break;
           case 1:
               waterRec.color = "red";
               waterText.text = "通水";
               logString[0] = "通水";
               break;
           default:
               break;
           }
           switch (lockArr.state.lidSta) {
           case 0:
               lidRec.color =  "transparent";
               lidText.text = "未改变";
               logString[1] = "未改变";
               break;
           case 1:
               lidRec.color = "red";
               lidText.text = "外力改变位置";
               logString[1] = "外力改变位置";
               break;
           default:
               break;
           }
           //lockCmd = 2 应答状态；
           lockArr.cmd = 2;
           voltageText.text = lockArr.state.voltage.toString() + "v";
       }
       //更新所在id的ArrItem;
       function updataLockArrItem(state, array) {
           var vol = hexTo10(state[16], state[17]);
           array.state.voltage = vol;
           array.state.waterSta = state[15] & 1;
           array.state.lidSta = (state[15] & (1 << 1)) >> 1;
       }
    UdpServer {
        id: udpServer;
        onLockStatusChanged: {
            console.log("lock status: " + status);
            console.log("lock ip: " + sender);
            console.log("lock port: " + senderPort);
            lockStatusparse(status, sender, senderPort);
        }
        //lock状态解析
        function lockStatusparse(sta, ip, port) {
            ipCombox.text = ip.toString();
            console.log("status = " + sta);
            //命令字为1的情况下接受的状态包
            if (sta[13] == 1) {
                var lockID = "";
                for (var i = 0; i < 12; i++) {
                    sendLockID[i] = sta[i];
                    lockID += sta[i].toString(16);
                }
                //更新idComboBox
                if (idCombox.find(lockID) == -1) {
                    idCombox.model.append({text: lockID});
                }
                idCombox.currentIndex = idCombox.find(lockID);
                //在arrItem里寻找id 如果没有则添加  有则更新
                var arr = findId(lockID);
                if (arr == null) {
                    console.log("arr == null");
                    arr = newLockArrItem(lockID, lockCmd, sta);
                } else {
                    console.log("arr == no null");
                    updataLockArrItem(sta, arr)
                }
                var recvString = "";
                for (i = 0; i < sta.length; i++) {
                    recvString += hexRepair(sta[i].toString(16)) + " ";
                }
                //更新界面状态显示
                updateSta(arr);
                recvString += "   " + logString[0] + " " + logString[1];
                recvLog(recvString);
                //应答包
                sendCommand(sendLockID, index, arr.cmd, "", ip, port);
            } else {
                return;
            }
        }
    }
}
